package com.test41;

/**
 * 上下文切换
 */
public class MyTest_01 {

    private static final long count = 100000000L;

    public static void main(String[] args) {
        concurrency();
        serial();
    }

    // 并行
    public static void concurrency() {
        long start = System.currentTimeMillis();
        Thread thread = new Thread(() -> {
            long a = 0;
            for (int i = 0; i < count; i++) {
                a += 5;
            }
        });
        thread.start();
        long b = 0;
        for (int i = 0; i < count; i++) {
            b--;
        }
        long end = System.currentTimeMillis();
        System.out.println("concurrency：time=" + (end-start) + "，b=" + b);
    }

    // 串行
    public static void serial() {
        long start = System.currentTimeMillis();
        long a = 0;
        for (int i = 0; i < count; i++) {
            a += 5;
        }
        long b = 0;
        for (int i = 0; i < count; i++) {
            b--;
        }
        long end = System.currentTimeMillis();
        System.out.println("serial：time=" + (end-start) + "，a=" + a + "，b=" + b);
    }
}
